<extend name="Layout/ins_page" />
<block name="content">
	{:W('PageHeader',array('name'=>'任务详情','search'=>'N'))}
	<div class="operate panel panel-default">
		<div class="panel-body">
			<div class="pull-left">
				<a onclick="go_return_url();" class="btn btn-sm btn-primary">返回</a>
			</div>
			<div class="pull-right">
				<eq name="no_assign" value="1">
					<a onclick="let_me_do({$task_id})" class="btn btn-sm btn-primary">我来处理</a>
				</eq>
			</div>
		</div>
	</div>
	<form method='post' id="form_data" name="form_data" enctype="multipart/form-data"   class="well form-horizontal">
		
		<input type="hidden" id="ajax" name="ajax" value="0" />
		<input type="hidden" id="executor" name="executor" value="{$vo.executor}" />
		<input type="hidden" id="opmode" name="opmode" value="add" />

		<div class="form-group">
			<label class="col-sm-2 control-label" for="name">标题：</label>
			<div class="col-sm-10">
				<p class="form-control-static">
					{$vo.name}
				</p>
			</div>
		</div>
		<div class="form-group col-sm-6">
			<label class="col-sm-4 control-label" for="name">发起人：</label>
			<div class="col-sm-8">
				<p class="form-control-static">
					{$vo.user_name}
				</p>
			</div>
		</div>
		<div class="form-group col-sm-6">
			<label class="col-sm-4 control-label" for="name">发起时间：</label>
			<div class="col-sm-8">
				<p class="form-control-static">
					{$vo.create_time|to_date}
				</p>
			</div>
		</div>
		<div class="form-group col-sm-6">
			<label class="col-sm-4 control-label" for="name">谁来执行：</label>
			<div class="col-sm-8">
				<p class="form-control-static">
					{$vo.executor|show_contact}
				</p>
			</div>
		</div>
		<div class="form-group col-sm-6">
			<label class="col-sm-4 control-label" for="expected_time">期望完成时间：</label>
			<div class="col-sm-8">
				<p class="form-control-static">
					{$vo.expected_time|substr=0,16}
				</p>
			</div>
		</div>

		<div class="form-group">
			<label class="col-sm-2 control-label" for="content">任务说明：</label>
			<div class="col-xs-10">
				<div class="task_content form-control-static">
					{$vo.content}
				</div>
			</div>
		</div>

		<div class="form-group">
			<label class="col-sm-2 control-label" for="name">附件：</label>
			<div class="col-sm-10">
				{:W('FileUpload/view',array('add_file'=>$vo['add_file'],'mode'=>'show'))}
			</div>
		</div>
	</form>

	<notempty name="task_log">
		<div class="ul_table border-bottom ul_table_responsive">
			<ul>
				<li class="thead">
					<span class="col-9 text-center">指派给</span>
					<span class="col-6 text-center">状态</span>
					<span class="col-6 text-center">执行人</span>
					<span class="col-13 text-center">计划完成时间</span>
					<span class="col-13 text-center">已完成情况</span>
					<span class="col-13 pull-right">附件</span>
					<span class="auto">执行内容</span>
				</li>
				<volist name="task_log" id="item">
					<li class="tbody">
						<span class="col-9 text-center">&nbsp;{$item.executor_name}</span>
						<span class="col-6 text-center">&nbsp;{$item.status|task_status}</span>
						<span class="col-6 text-center">&nbsp;{$item.transactor_name}&nbsp;</span>
						<span class="col-13 text-center">&nbsp;{$item.plan_time|substr=0,16}&nbsp;</span>
						<span class="col-13 text-center"> &nbsp;
							<eq name='item.status' value='10'>
								{$item.finish_rate|finish_rate}
							</eq>
							<egt name='item.status' value='20'>
								{$item.finish_time|substr=0,16}
							</egt>&nbsp;
						</span>
						<span class="col-13 pull-right">
							{:W('FileUpload/link',array($item['add_file']))}
						</span>
						<span class="auto">
							<div style="overflow:hidden">
								{$item.feed_back}
							</div>
						</span>
					</li>
				</volist>
			</ul>
		</div>
	</notempty>
	<notempty name="task_product">
		<div class="ul_table border-bottom ul_table_responsive">
			<ul>
				<li class="thead">
					<span class="col-9 text-center">产品</span>
					<span class="col-6 text-center">数量</span>
					<span class="col-8 text-center">生产单价</span>
					<span class="col-20 text-center">创建时间</span>
				</li>
				<volist name="task_product" id="item">
					<li class="tbody">
						<span class="col-9 text-center">&nbsp;{$item.product}</span>
						<span class="col-6 text-center">&nbsp;{$item.number}</span>
						<span class="col-8 text-center">&nbsp;{$item.product_price}</span>
						<span class="col-20 text-center">&nbsp;{$item.create_at}</span>
					</li>
				</volist>
			</ul>
		</div>
	</notempty>
	<notempty name="task_product_log">
		<div class="ibox task_todo">
			<div class="ibox-title">
				<h5 class="smaller">指派人完成的产量</h5>
			</div>
		</div>
		<div class="ul_table border-bottom ul_table_responsive">
			<ul>
				<li class="thead">
					<span class="col-9 text-center">姓名</span>
					<span class="col-9 text-center">产品</span>
					<span class="col-6 text-center">数量</span>
					<span class="col-10 text-center">生产单价</span>
					<span class="col-10 text-center">完成总价</span>
					<span class="col-20 text-center">完成时间</span>
					<span class="col-10 text-center">操作</span>
				</li>
				<volist name="task_product_log" id="item">
					
					<li class="tbody task-product-log-list" log-id="{$item.id}" >
						<span class="col-9 text-center">{$item.user_name}</span>
						<span class="col-9 text-center">{$item.product}</span>
						<span class="col-6 text-center ">
							<input type="text" name="number" value="{$item.number}" class="col-6 task_product-number" disabled />
						</span>
						<span class="col-10 text-center">{$item.product_price}</span>
						<span class="col-10 text-center">{$item.total_product_price}</span>
						<span class="col-20 text-center">{$item.create_at}</span>
						<if condition="$item['is_in'] eq 0">
							<span class="col-10 text-center edit-log">
								
								<if condition="($user_id eq $item['parent_user_id'])">
									<a href="javascript:;" class="product-log-edit" log-id="{$item.id}" user-name="{$item.user_name}" product="{$item.product}">修改</a>
									
									<a href="javascript:;" class="product-log-delete" log-id="{$item.id}" user-name="{$item.user_name}" product="{$item.product}">删除</a>
								</if>
								
								<if condition="($user_id eq $vo['check_user_id']) AND ($vo['is_check_in'] eq 1) ">
									<a href="javascript:;" class="product-log-check-in in-storehoust-log" log-id="{$item.id}" user-name="{$item.user_name}" product="{$item.product}" >入库</a>		
								</if>
							</span>
						<else />
							<span class="col-10 text-center">成功入库</span>
						</if>
					</li>
				</volist>
			</ul>
		</div>
	</notempty>
	
	<eq name="is_working" value="1">
		<notempty name="task_product">
			<div class="ibox task_todo">
				<div class="ibox-title">
					<h5 class="smaller">当天完成任务</h5>
				</div>
			</div>
			<div class="ibox-content parent-task-product">
				<form method='post' id="form_product_data" name="form_product_data" enctype="multipart/form-data" class="well form-horizontal" >
					<input type="hidden" name="task_id" value="{$task_id}"/>
					<div class="form-group parent-add-task-product">
						<label class="col-sm-2 control-label" for="name">生产产品：</label>
						<div class="add-task-product">
							<div class="col-sm-2" id="task-product-select">
								<select class="form-control" name="product_id[]" id="is_del"  check="require" msg="请输入当天完成产品" >
										<option  value="">请选择</option>
									<volist name="task_product" id="item">
										<option  value="{$item.id}">{$item.product}</option>
									</volist>
								</select>
								<input class="form-control" type="text"  name="number[]" placeholder="数量"  check="require" msg="输入当天完成产品数量" />
							</div>
							<a class="btn btn-sm btn-primary product-add" >添加</a>
						</div>
					</div>
					<div class="form-group">
						<div class="action col-sm-2 col-sm-offset-2">
							<input class="btn btn-sm btn-primary" type="button" value="保存" onclick="save_product()" />
							<a class="btn btn-sm btn-default" type="button" href="javascript:;" onClick="javascript:history.go(-1);" />取消</a>
						</div>
					</div>
				</form>
			</div>
		</notempty>
		<div class="ibox task_todo">
			<div class="ibox-title">
				<h5 class="smaller">我的执行情况</h5>
				<div class="ibox-tools no-border">
					<ul class="nav nav-tabs" id="myTab">
						<li class="active">
							<a href="#working" data-toggle="tab">执行情况</a>
						</li>
						<li>
							<a href="#forword" data-toggle="tab">转交任务</a>
						</li>
						<li>
							<a href="#reject" data-toggle="tab">拒绝接受</a>
						</li>
					</ul>
				</div>
			</div>
			<div class="ibox-content">
				<div class="tab-content">
					<div class="tab-pane active" id="working">
						<form method='post' id="form_data_working" name="form_data_working">
							<input type="hidden" name="status" value="10" />
							<input type="hidden" name="id" value="{$task_working.id}" />
							<div class="form-group col-sm-6">
								<label class="col-sm-4 control-label" >计划完成时间：</label>
								<div class="col-sm-8">
									<input class="input-date-time form-control" name="plan_time" value="{$task_working.plan_time|substr=0,16}" check="require" msg="请输入计划完成时间" readonly="readonly" />
								</div>
							</div>
							<div class="form-group col-sm-6">
								<label class="col-sm-4 control-label" >完成率：</label>
								<div class="col-sm-8">
									<div class="form-control-static" >
										<input type="hidden" id="finish_rate" name="finish_rate"/>
										<div  class="slider_box" >
											<div id="basic_slider"></div>
											<div class="right" id="basic_slider_val">
												{$task_working.finish_rate|default=0}%
											</div>
										</div>
									</div>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">执行内容：</label>
								<div class="col-sm-10">
									<p>
										<textarea id="feed_back" name="feed_back" class="col-xs-12 simple " style="height:120px">{$task_working.feed_back}</textarea>
									</p>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label" for="name">附件：</label>
								<div class="col-sm-10">
									{:W('FileUpload/edit',array($task_working['add_file']))}
									<p></p>
								</div>
							</div>
						</form>
					</div>
					<div class="tab-pane" id="forword">
						<form method='post' id="form_data_forword" name="form_data_forword" >
							<input type="hidden" name="id" value="{$task_working.id}">
							<input type="hidden" name="task_id" value="{$task_working.task_id}" />
							<input type="hidden" name="status" value="21" />
							<input type="hidden" id="forword_executor" name="forword_executor" />
							<div class="form-group col-sm-12">
								<label class="col-sm-2 control-label" for="name">转交给：</label>
								<div class="col-sm-10">
									<div id="actor_wrap" class="inputbox">
										<a class="pull-right btn btn-link text-center" onclick="popup_actor();"><i class="fa fa-user"></i> </a>
										<div class="wrap" >
											<span class="address_list"></span>
											<span class="text" >
												<input class="letter" type="text" />
											</span>
										</div>
										<div class="search dropdown">
											<ul class="dropdown-menu"></ul>
										</div>
									</div>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">执行内容：</label>
								<div class="col-sm-10">
									<textarea id="feed_back" name="feed_back" class="col-xs-12 simple" style="height:120px" check="require" msg="请输入执行内容">{$task_working.feed_back}</textarea>
									<p></p>
								</div>
							</div>
						</form>
					</div>
					<div class="tab-pane" id="reject">
						<form method='post' id="form_data_reject" name="form_data_reject" >
							<input type="hidden" name="status" value="22" />
							<input type="hidden" name="id" value="{$task_working.id}" />
							<div class="form-group">
								<label class="col-sm-2 control-label">执行内容：</label>
								<div class="col-sm-10">
									<textarea id="feed_back" name="feed_back" class="col-xs-12 simple" style="height:120px" >{$task_working.feed_back}</textarea>
									<p></p>
								</div>
							</div>
						</form>
					</div>
				</div>
				<div class="widget-toolbox clearfix">
					<div class="pull-left col-sm-8 col-sm-offset-2">
						<a onclick="save_log();" class="btn btn-sm btn-primary col-6">提交</a>
						<a class="btn btn-sm btn-default" type="button" href="javascript:;" onClick="javascript:history.go(-1);" />取消</a>
					</div>
				</div>
			</div>
		</div>
		
	</eq>
</block>
<block name="js">
	<script type="text/javascript">
		$(function() {
			show_content();
		});

		function popup_actor() {
			winopen("{:U('popup/task')}",560, 470);
		}

		function accept($task_log_id) {
			sendAjax("{:U('accept')}", 'task_log_id=' + $task_log_id, function(data) {
				if (data.status) {
					ui_alert(data.info, function() {
						location.reload(true);
					});
				}
			});
		}

		function let_me_do($task_id) {
			sendAjax("{:U('let_me_do')}", 'task_id=' + $task_id, function(data) {
				if (data.status) {
					ui_alert(data.info, function() {
						location.reload(true);
					});
				}
			});
		}

		function save_log() {
			
			$status_name = $(".task_todo .tab-pane.active").attr('id');
			if ($status_name == "forword") {
				if ($(".address_list span").length == 0) {
					ui_error('请选择要转交的人');
					return false;
				} else {
					$("#forword_executor").val("");
					$("#actor_wrap span.address_list span").each(function() {
						$("#forword_executor").val($("#forword_executor").val() + $(this).find("b").text() + '|' + $(this).attr("data") + ";");
					});
				}
			}
			$form_name = "form_data_" + $status_name;
			if (check_form($form_name)) {
				var vars = $("#" + $form_name).serialize();
				if ($status_name == "working") {
					sendAjax("{:U('save_log')}", vars, function(data) {
						if (data.status) {
							ui_alert(data.info, function() {
								location.reload(true);
							});
						} else {
							ui_error(data.info);
						}
					});
				}

				if ($status_name == "forword") {
					ui_confirm('转交以后不能修改了,是否提交?', function() {
						sendAjax("{:U('save_log')}", vars, function(data) {
							if (data.status) {
								ui_alert(data.info, function() {
									location.reload(true);
								});
							} else {
								ui_error(data.info);
							}
						});
					});
				}

				if ($status_name == "reject") {
					ui_confirm('选择拒绝以后不能修改了,是否提交?', function() {
						sendAjax("{:U('save_log')}", vars, function(data) {
							if (data.status) {
								ui_alert(data.info, function() {
									location.reload(true);
								});
							} else {
								ui_error(data.info);
							}
						});
					});
				}
			}
		}
		
		/**
		   保存生产了多少产品
		*/
		function save_product($task_id) {
			
			ui_confirm('今天的生产产品,是否提交?', function() {
				var vars = $("#form_product_data").serialize();
				sendAjax("{:U('save_product')}", vars, function(data) {
					if (data.status) {
						ui_alert(data.info, function() {
							location.reload(true);
						});
					} else {
						ui_error(data.info);
					}
				});	
			});
		}

		$(document).ready(function() {
			
			var task_product_select = $('#task-product-select').html();
			
			$(document).on("click", ".inputbox .address_list a.del", function() {
				$(this).parent().parent().remove();
			});
			
			if($("#basic_slider").size()>0) {
				
				$("#basic_slider").noUiSlider({
					start : parseInt('{$task_working.finish_rate|default=0}'),
					step : 1,
					behaviour : 'tap',
					connect : 'upper',
					range : {
						'min' : 0,
						'max' : 100,
					},
				});
				
				$("#basic_slider").on('slide', function() {
					$("#finish_rate").val($(this).val());
					$("div.slider_box .right").text(Math.abs($(this).val()) + "%");
				});
			}
			

			set_val('status', '{$task_working.finish_rate}');
			
			$("#slider-range-priority").slider({
				range : "min",
				min : 0,
				max : 100,
				value : '{$task_working.finish_rate}',
				slide : function(event, ui) {
					$("#finish_rate").val(ui.value);
					$("#slider_val").text(ui.value + "%");
				}
			});
			
			$('.form-group').on('click','.product-add',function(){ //添加
				
				var html = '<div class="form-group">'
							+'<label class="col-sm-2 control-label" for="name">生产产品：</label>'
							+'<div class="add-task-product">'
							+'<div class="col-sm-2">'
							+ task_product_select
							+'</div>'
							+'<span class="btn btn-sm btn-primary product-del">删除</span>'
							+'</div>'
							+'</div>';
				$('.parent-add-task-product').after(html);
			});
			
			$('.parent-task-product').on('click','.product-del',function(event){ //删除
				$(this).parents('.form-group').remove();
			})
			
			//删除下面的人提交的生产任务
			$('.task-product-log-list').on('click','.product-log-delete',function(event) {
				
				var _this = $(this);
				var log_id = _this.attr('log-id');
				var user_name = _this.attr('user-name');
				var product = _this.attr('product');
				var reason = '确定删除'+user_name+'生产的'+product+'记录?';
				ui_confirm(reason, function() {
					var vars = $("#form_product_data").serialize();
					sendAjax("{:U('delete_product_log')}",'log_id='+log_id, function(data) {
						if (data.status) {
							_this.parents('.task-product-log-list').remove();
						} else {
							ui_error(data.info);
						}
					});	
				});
			})
			
			// 编辑下面的人提交的工作任务
			$('.task-product-log-list').on('click','.product-log-edit',function(event) {
				
				var _this = $(this);
				var log_id = _this.attr('log-id');
				var user_name = _this.attr('user-name');
				var product = _this.attr('product');
				var _editnum = _this.parents('.task-product-log-list').find('.task_product-number');
				_editnum.removeAttr('disabled');
				_this.removeClass('product-log-edit').addClass('product-log-save').text('保存');
			})
			
			// 保存下面的人提交的工作任务
			$('.task-product-log-list').on('click','.product-log-save',function(event) {
				
				var _this = $(this);
				var log_id = _this.attr('log-id');
				var user_name = _this.attr('user-name');
				var product = _this.attr('product');
				var reason = '确定修改'+user_name+'生产的'+product+'记录?';
				var _editnum = _this.parents('.task-product-log-list').find('.task_product-number');
				var number = _editnum.attr('value');
				ui_confirm(reason, function() {
					var vars = $("#form_product_data").serialize();
					sendAjax("{:U('edit_product_log')}",'log_id='+log_id+'&number='+number, function(data) {
						if (data.status) {
							_editnum.attr('disabled');
							_this.addClass('product-log-edit').removeClass('product-log-save').text('编辑');
						} else {
							ui_error(data.info);
						}
					});	
				});
			})
			
			// 入库下面的产品
			$('.task-product-log-list').on('click','.product-log-check-in',function(event) {
				
				var _this = $(this);
				var log_id = _this.attr('log-id');
				var reason = '确定入库吗';
				ui_confirm(reason, function() {
					sendAjax("{:U('in_storehouse')}",'log_id='+log_id, function(data) {
						if (data.status) {
						    $('.in-storehoust-log').removeClass('product-log-check-in').text('成功入库');	
						} else {
							ui_error(data.info);
						}
					});	
				});
			})
			
		});
	</script>
</block>
